// ----------------------- Name Space and Environment Initialization
// Create Namespace "listr.main"
listr.main = {};

// KeyPress Handling Boolean Enabler
listr.main.keypressHandlingEnabled = true;

//Mapping the KeyPress Events
$(document).keypress( listr.main.keypressEventHandling );

//----------------------- Functions Definition
// Keypress Event Handling Function
listr.main.keypressEventHandling = function(event) {
	if (listr.main.keypressHandlingEnabled) {
		switch( String.fromCharCode(event.which) ) {
			case 'n':
				$('#new_list_button').click();
				break;
		}
	}
}

listr.main.CreateNewListBoxy = function() {
	new Boxy($('#new_list_hidden_form'), 
			{
				title: 'Type the Title of a New List',
				modal: true,
				draggable: false,
				closeText : '',
				afterShow : function() {
					$('#new_list_hidden_form_title').focus();
					// Disable KeyPress Handling
					listr.main.keypressHandlingEnabled = false;
				},
				afterHide : function() {
					// Enabling KeyPress Handling
					listr.main.keypressHandlingEnabled = true;
				}
			});
}

listr.main.ChangeListsVisibility = function(newVisibility) {
	// Calculating the number of lists to update
	var lists_to_update_num = $('.current_list_checkbox input:checkbox[name=\'list_id[]\']:checked').length;
	
	// Calculating the IDs of the lists to update
	var list_ids = [];
	$('.current_list_checkbox input:checkbox[name=\'list_id[]\']:checked').each( function() {
		list_ids[ list_ids.length ] = $(this).val();
	});
	
	// Disable KeyPress Handling
	listr.main.keypressHandlingEnabled = false;
	
	// Boxy.ask for confirmation
	Boxy.ask("Are you sure you want to make " +lists_to_update_num+ " List(s) <span class=\"" +newVisibility+ "\">" +newVisibility+ "</span>?",
			["Yes", "No"],
			function(answer) {
				// Check that the answer of the user is "Yes" and the number of list to operate on is ">0" (avoid useless requests)
				if ( answer == "Yes" && lists_to_update_num > 0 ) {
					// "display: block" the "#busy_status" and put it as html of the "#current_lists"
					$('#current_lists').html($('#current_lists #busy_status').css('display', 'block'));
					
					// Execute/Submit the AjaxPost call/request
					$.post('/list/update/visibility',
							{
								'visibility'	: newVisibility,
								'list_id[]'		: list_ids,
								'ajax'			: 'true'
							},
							function(ajaxPostResultHtml) {
								// Hide "#current_list"
								$('#current_lists').hide();
								// Populate "#current_lists" with the result
								$('#current_lists').html( ajaxPostResultHtml );
								// FadeIn Slowly
								$('#current_lists').fadeIn("slow");
								// Enable KeyPress Handling
								listr.main.keypressHandlingEnabled = true;
							}
					);
				};
			},
			{
				title: 'Do you Confirm?',
				modal: true,
				draggable: false,
				closeText : '',
				afterShow : function() {
					listr.keypressHandlingEnabled = false;
				},
				afterHide : function() {
					listr.keypressHandlingEnabled = true;
				}
			}
	);
}

listr.main.ChangeListsVisibilityPublic = function() {
	listr.main.ChangeListsVisibility("public");
}

listr.main.ChangeListsVisibilityPrivate = function() {
	listr.main.ChangeListsVisibility("private");
}
